var express = require('express')
var router = express.Router()
const { getAll, getById, exec } = require('../db/index')

// 解构调试的的标记,将来会根据这个决定是否发送错误信息
const { debug } = require('../config/index')

// 获取所有的分类
router.get('/', async function (req, res) {
  // 操作数据库三部曲
  //1,获取请求的参数
  //2,编写sql语句
  //3,执行sql语句,返回结果
  // let sql = 'select * from category'
  let sql = `select 
              t2.sum as value,t1.tag as name
              from 
	            category t1,(SELECT cate_id,COUNT(cate_id) sum FROM articles GROUP BY cate_id) t2
              where
	            t1.id=t2.cate_id `

  try {
    const data = await getAll(sql)
    res.send({
      code: 0,
      message: '获取所有的代办成功',
      result: data,
    })
  } catch (error) {
    res.send({
      code: 100101,
      message: '获取所有代办失败',
      result: debug ? error : '',
    })
  }
})

// 获取每个月份下的文章产量
router.get('/output',async function(req,res){
  let sql = `SELECT 
              MONTH(created_time) as month,count(*) as count 
              from articles
              GROUP BY MONTH(created_time)`
  
  const data = await getAll(sql)
  res.send({
    code:0,
    message:'查询成功',
    result:data
  })
})

module.exports = router
